﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling;
using System.Drawing.Imaging;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Render;
using System.IO;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations {
    public class OperationGetModelWmf : IOperation {

        private byte[] wmfBytes;

        public byte[] WmfBytes {
            get {
                return wmfBytes;
            }
        }

        private int modelId;
        public OperationGetModelWmf(int modelId) {
            this.modelId = modelId;
        }
        #region IOperation Members

        public string Name {
            get {
                return "Получение Wmf модели";
            }
        }

        public OperationGroup OperationGroup {
            get {
                return OperationGroup.Other;
            }
        }

        public System.Drawing.Image Thumbnail {
            get {
                return null;
            }
        }

        public void Execute(ModelControl modelControl) {
            ModelCoordinateConverter coordinateConverter = new ModelCoordinateConverter();
            DraftRender render = new DraftRender();

            foreach(ModelCell modelCell in modelControl.ModelContainer.InnerComponents) {
                if(modelCell.Model.SerialNumber == this.modelId) {
                    render.SetDimensionsPreferences(GlobalPreferences.Preferences.DraftDimensions);

                    RectangleF modelRect = modelCell.Model.BoundingBox;
                    ModelPoint center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                    RectangleF screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                    Bitmap bitmap = new Bitmap((int)screenRect.Width, (int)screenRect.Height);
                    Graphics ig = Graphics.FromImage(bitmap);

                    render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                    render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);

                    modelRect = render.GetModelBoundingBoxAfterDrawing(modelCell, (int)screenRect.Width, (int)screenRect.Height);
                    center = new ModelPoint(modelRect.X + modelRect.Width / 2, modelRect.Y + modelRect.Height / 2);
                    screenRect = coordinateConverter.ModelRectangleToScreenRectangle(modelRect, (int)modelRect.Width, (int)modelRect.Height, modelRect, center);
                    render.Initialize(ig, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                    render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);
                    render.Visit(modelCell);
                    IntPtr hdc = ig.GetHdc();
                    MemoryStream stream = new MemoryStream();

                    Metafile metafile = new Metafile(stream, hdc, screenRect, MetafileFrameUnit.Pixel, EmfType.EmfPlusOnly);
                    ig.ReleaseHdc();
                    Graphics mg = Graphics.FromImage(metafile);
                    mg.PageUnit = GraphicsUnit.Pixel;
                    mg.Clear(Color.White);
                    render.Initialize(mg, (int)modelRect.Width, (int)modelRect.Height, modelRect, coordinateConverter, center);
                    render.SetSizesFont(GlobalPreferences.Preferences.DraftDimensions.Font);
                    render.Visit(modelCell);
                    
                    
                    
                    mg.Dispose();
                    ig.Dispose();
                    wmfBytes = stream.ToArray();
                    stream.Close();
                    return;
                }
            }
        }

        public bool CanUndo {
            get {
                return false;
            }
        }

        #endregion
    }
}
